package com.accompany.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.accompany.R;
import com.accompany.main.BaseApplication;
import com.bumptech.glide.Glide;

import butterknife.ButterKnife;
/**
 * Created by ${wangjiang} on 2017/9/1.
 * 所有activity 的基类，并封装了简单的动画效果
 * @author Administrator
 */
public abstract class BaseActivity extends AppCompatActivity {
    public Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ((BaseApplication) getApplication()).addActivity(this);
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        ButterKnife.bind(this);
        mContext = this;
        initLayoutView();
//        hideBottomUIMenu();
    }
    private void hideBottomUIMenu() {
//隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    /**
     * 获取布局文件
     */
    public abstract int getLayoutId();
    /**
     * 初始化view
     */
    public abstract void initLayoutView();
    /**
     * 通过类名启动Activity带动画
     * @param pClass
     */
    public void openActivityWitchAnimation(Class<?> pClass) {
        openActivityWitchAnimation(pClass, null);
    }
    /**
     * 通过类名启动Activity，并且含有Bundle数据,带动画
     * @param pClass
     * @param pBundle
     */
    public void openActivityWitchAnimation(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(intent);
        //跳转动画
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        }
    }
    /**
     * 通过类名启动Activity 不带动画
     * @param pClass
     */
    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }
    /**
     * 通过类名启动Activity，
     * 并且含有Bundle数据不包含动画
     * @param pClass
     * @param pBundle
     */
    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(intent);
    }
    /**
     * 带动画的退出
     */
    public void finishWitchAnimation() {
        finish();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        }
    }
    /**
     * 托付OnKeyDown事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishWitchAnimation();
        }
        return super.onKeyDown(keyCode, event);
    }
    /**
     * 返回按钮的实现
     *
     * @param view 传入的返回View
     */
    public void BackClick(View view) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishWitchAnimation();
            }
        });
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        //当被销毁后，将其移除
        BaseApplication.removeActivity(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1&&!this.isDestroyed()) {
            Glide.with(this).pauseRequests();
        }
    }
}
